package com.abapi.pay.core.biz;

import cn.hutool.core.util.HexUtil;
import com.abapi.pay.core.constans.MessageType;
import com.abapi.pay.core.dto.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @Author ldx
 * @Date 2019/10/30 18:14
 * @Description
 * @Version 1.0.0
 */
public class Decode {

    public static MessageDTO decode(byte[] b){
        String msg = new String(b);
        String decodeMsg = HexUtil.decodeHexStr(msg);
        MessageDTO messageDTO = JSON.parseObject(decodeMsg,MessageDTO.class);
        if(messageDTO.getType().equals(MessageType.ALI_BIZ)){
            JSONObject jsonObject = (JSONObject) messageDTO.getBody();
            AliBizDTO aliBizDTO = jsonObject.toJavaObject(AliBizDTO.class);
            messageDTO.setBody(aliBizDTO);
        }else if(messageDTO.getType().equals(MessageType.WX_BIZ)){
            JSONObject jsonObject = (JSONObject) messageDTO.getBody();
            WxBizDTO wxBizDTO = jsonObject.toJavaObject(WxBizDTO.class);
            messageDTO.setBody(wxBizDTO);
        }else if(messageDTO.getType().equals(MessageType.REGISTER)){
            JSONObject jsonObject = (JSONObject) messageDTO.getBody();
            RegisterDTO registerDTO = jsonObject.toJavaObject(RegisterDTO.class);
            messageDTO.setBody(registerDTO);
        }else if(messageDTO.getType().equals(MessageType.SUCCESS)){
            JSONObject jsonObject = (JSONObject) messageDTO.getBody();
            SuccessDTO successDTO = jsonObject.toJavaObject(SuccessDTO.class);
            messageDTO.setBody(successDTO);
        }

        return messageDTO;
    }

}
